package com.proj.hot.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.activity.LoginActivity;
import com.proj.hot.activity.VODBuyPlayActivity;
import com.proj.hot.activity.webview.WebMainActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物
 * Created by zangchenggang on 17/6/30.
 */
public class chaFragment extends BaseFragment {

    private List<View> viewList = new ArrayList<>();
    private View shop_view1,shop_view2,shop_view3;
    private ImageView shop_poster1;
    JSONArray dataArray;
    String code;




    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {


            }
        }
    };


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (code != null) {
            getData();
        }
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_cha, container, false);
        shop_view1 = rootView.findViewById(R.id.shop_rl_poster1);
        shop_view2 = rootView.findViewById(R.id.shop_rl_poster2);
        shop_view3 = rootView.findViewById(R.id.shop_rl_poster3);

        viewList.add(shop_view1);
        viewList.add(shop_view2);
        viewList.add(shop_view3);


        shop_poster1 = (ImageView) rootView.findViewById(R.id.shop_poster1);





        setOnListener();
        return rootView;
    }

    private void setOnListener() {

        for (int i = 0; i < viewList.size(); i++) {
            final int k = i;
            viewList.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // if jsonarray type=13

                    String url="";

//                    查模块：
//                    快捷查询=》page/1.0.9.html
//                    法律服务机构查询=》page/1.0.1.html
//                    法律服务人员查询=》page/1.0.2.html

                    if(k==0){

                        url=GlobalParams.HTML_PATH+"page/1.0.9.html";
                    }

                    if(k==1){

                        url=GlobalParams.HTML_PATH+"page/1.0.1.html";
                    }


                    if(k==2){

                        url=GlobalParams.HTML_PATH+"page/1.0.2.html";
                    }


                    Intent intent = new Intent(getActivity().getApplicationContext(), WebMainActivity.class);
                    intent.putExtra("weburl", url);
                    startActivity(intent);
                }
            });
        }
    }


    public void setCodeAndInitData(String code) {

        this.code = code;

    }


    private void getData() {

        try {


            /**
             * ---------------body------------------
             */
            JSONObject body = new JSONObject();
            body.put("code", code);
            body.put("sn", GlobalParams.SN);

            RequestParams params = new RequestParams(GlobalParams.reqHost + "featuredFirst/getDetails?"+code);
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        JSONObject jData = new JSONObject(s);

                        JSONObject respbody = jData.getJSONObject("respbody");
                        dataArray = respbody.getJSONArray("data");
                        handler.sendEmptyMessage(0);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {

                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }

    }





}
